﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using NLBS.Entity;
using NLBS.Data;
using NLBS.Framework;

namespace NLBS.Control
{
    public partial class commentForm : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
         
           
        }


        protected void  InitMsgBox()
        {
            switch (Action)
            {
                case MessageType.Comment:
                case MessageType.GusetBook:
                    
                    break;
                case MessageType.EditGuestBook:
                    BindEditGuestBook();
                    break;
                case MessageType.EditComment:
                    BindEditComment();
                    
                    break;
            }

            btnSubmit.ServerClick += new EventHandler(btnSubmit_ServerClick);
        }

        void btnSubmit_ServerClick(object sender, EventArgs e)
        {
            if (Action == MessageType.Comment)
            {
                Comment comment = new Comment();
                comment.Author = comm_username.Value;
                comment.Content = message.Value;
                comment.AuthorID = 0;
                comment.LogID = Convert.ToInt32(Request.QueryString["id"]);
                comment.PostTime = DateTime.Now;
                comment.IP = Request.UserHostAddress;
                comment.EditMark = string.Empty;
                comment.UbbFlags = "1111";
                comment.Hidden = false;
                DbProvider.Instance().InsertComment(comment);
                Response.Redirect(string.Format("comment.aspx?act=save&logid={0}", comment.LogID.ToString()));
            }
            else if (Action == MessageType.GusetBook)
            {
                GuestBook guestBook = new GuestBook();
                guestBook.UserName = comm_username.Value;
                guestBook.UserID = 0;
                guestBook.Content = message.Value;
                guestBook.EditMark = string.Empty;
                guestBook.UbbFlags = "1111";
                guestBook.PostTime = DateTime.Now;
                guestBook.ReplyUserName = string.Empty;
                guestBook.Reply = string.Empty;
                guestBook.ReplyTime = DateTime.Now;
                guestBook.Hidden = false;
                guestBook.IP = Request.UserHostAddress;
                DbProvider.Instance().InsertGuestBook(guestBook);
               
                Response.Redirect("gbook.aspx?act=save");

            }
            else if (Action == MessageType.EditGuestBook)
            {
               // GuestBook.Reply=reply.Value;
                GuestBook.ReplyTime = DateTime.Now;
                DbProvider.Instance().UpdateGuestBook(GuestBook);
                Response.Redirect("gbook.aspx?act=update");
            }
            else if (Action == MessageType.EditComment)
            {
                Comment.Content = message.Value;
                DbProvider.Instance().UpdateComment(Comment);
                Response.Redirect("comment.aspx?act=update&logid="+Comment.LogID.ToString());
            }
        }

        private void BindEditGuestBook()
        {
            if (Request.QueryString["act"] != null)
            {
                if (Request.QueryString["act"] == "edit")
                {
                    Action = MessageType.EditGuestBook;
                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    GuestBook = DbProvider.Instance().GetGuestBookByID(id);
                    message.Value = GuestBook.Content;
                    //reply.Value = GuestBook.Reply;
                }
            }
        }

        private GuestBook _guestBook;

        public GuestBook GuestBook
        {
            set
            {
                _guestBook = value;
            }
            get
            {
                return _guestBook;
            }

        }

        private Comment _comment;

        public Comment Comment
        {
            set
            {
                _comment = value;
            }
            get
            {
                return _comment;
            }

        }

        

        private void BindEditComment()
        {
            if (Request.QueryString["act"] != null)
            {
                if (Request.QueryString["act"] == "edit")
                {
                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    Comment = DbProvider.Instance().GetCommentByID(id);
                    message.Value = Comment.Content;
                }
            }
        }
        private MessageType _action;
        public MessageType Action
        {
            get { return _action; }
            set { _action = value; }
        }
    }

    public enum MessageType
    {
        Comment,
        GusetBook,
        EditComment,
        EditGuestBook
    }
}

        //void btnSubmit_ServerClick(object sender, EventArgs e)
        //{
        //    int id = Convert.ToInt32(Request.QueryString["id"]);
        //    Comment editComment = DbProvider.Instance().GetCommentByID(id);
        //    editComment.Content=entry.Value;
        //    DbProvider.Instance().UpdateComment(editComment);
        //    Response.Redirect("comment.aspx?act=update?logid=" + editComment.LogID.ToString());
        //}